package com.baijiayun.liveuibase.widgets.courseware;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;

public class BaseCourseWareUploadDialog extends Dialog {
    private Context mContext;
    private View mView;
    private IOnChooseUploadTypeListener onChooseUploadTypeListener;

    public BaseCourseWareUploadDialog(@NonNull Context context, IOnChooseUploadTypeListener onChooseUploadTypeListener) {
        super(context);
        mContext = context;
        this.onChooseUploadTypeListener = onChooseUploadTypeListener;
    }

    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setGravity(Gravity.CENTER);//设置dialog显示居中
            dialogWindow.setWindowAnimations(R.style.BJYPushInOutFromCenter);
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.uibase_courseware_upload_dialog_layout, null);
        setContentView(mView);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) getContext().getResources().getDimension(R.dimen.bjy_base_course_file_upload_window_width);
        lp.height = (int) getContext().getResources().getDimension(R.dimen.bjy_base_course_file_upload_window_height);
        getWindow().setAttributes(lp);
        getWindow().setBackgroundDrawable(new DrawableBuilder()
                .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_window_bg_color))
                .cornerRadius(getContext().getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                .build());
        setCanceledOnTouchOutside(true);//点击外部Dialog消失

        initView();
    }

    private void initView() {
        mView.findViewById(R.id.base_courseware_upload_close_window).setOnClickListener(v -> dismiss());
        mView.findViewById(R.id.base_courseware_upload_static_container).setOnClickListener(v -> {
            if (onChooseUploadTypeListener != null) {
                onChooseUploadTypeListener.onChooseStatic();
            }
            dismiss();
        });
        mView.findViewById(R.id.base_courseware_upload_anim_container).setOnClickListener(v -> {
            if (onChooseUploadTypeListener != null) {
                onChooseUploadTypeListener.onChooseAnim();
            }
            dismiss();
        });
    }

    /**
     * 上传类型选择监听
     */
    public interface IOnChooseUploadTypeListener {
        // 静态
        void onChooseStatic();

        // 动态
        void onChooseAnim();
    }
}
